import { loadCss, loadModules } from 'esri-loader'
import arcgisConfig from "../../url/argics.js";
import clickView from "./click.js";
export default function createMap(doc, obj) {
    return new Promise(function(resolve, reject) {
        const options = {
            url: arcgisConfig.getArgicsJs
        };
        loadCss(arcgisConfig.getArcgisCss);
        loadModules([
            "esri/Map",
            "esri/views/SceneView",
            "esri/layers/TileLayer",
            "esri/geometry/Extent",
            "esri/layers/FeatureLayer",
            "esri/layers/MapImageLayer",
            "esri/core/watchUtils",
            "esri/geometry/Polyline",
            "esri/geometry/Polygon",
            "esri/Graphic",
            "esri/layers/GraphicsLayer",
            'esri/layers/ElevationLayer',
            'esri/layers/BaseElevationLayer',
            'esri/widgets/LineOfSight/LineOfSightViewModel',
            'esri/geometry/geometryEngine',
            'esri/geometry/Point',
            'esri/geometry/support/webMercatorUtils',
            'esri/tasks/Geoprocessor',
            'esri/tasks/support/FeatureSet',
            'esri/tasks/support/LinearUnit',
            "esri/PopupTemplate",
            "esri/widgets/Sketch/SketchViewModel",
            "esri/layers/GeoJSONLayer",
            "esri/symbols/LineSymbol3DLayer",
            "esri/symbols/LineSymbol3D",
            "esri/layers/WebTileLayer",
            "dojo/domReady!",
        ], options).then(function([Map, SceneView, TileLayer, Extent, FeatureLayer, MapImageLayer, watchUtils, Polyline, Polygon, Graphic, GraphicsLayer, ElevationLayer, BaseElevationLayer,
            LineOfSightViewModel, geometryEngine, Point, webMercatorUtils, Geoprocessor, FeatureSet, LinearUnit, PopupTemplate, SketchViewModel, GeoJSONLayer, LineSymbol3DLayer,
            LineSymbol3D, WebTileLayer
        ]) {
            var yxdt = new TileLayer({
                    url: arcgisConfig["yxdt"],
                    legendEnabled: false,
                })
                // let elevstionLayer = new ExaggeratedElevationLayer();
            let map = new Map({
                layers: [yxdt],
                ground: "world-elevation"
            });
            let extent = new Extent({
                xmax: 11535663.309163755,
                xmin: 10446284.449691014,
                ymax: 4928826.2609874215,
                ymin: 4230182.952537651,
                spatialReference: {
                    wkid: 102100
                }
            });
            let view = new SceneView({
                map: map,
                container: doc,
                camera: {
                    fov: 65,
                    heading: 10.000080379357879,
                    tilt: 0,
                    position: [99.11036437218115, 38.2071236280878, 1028840.233703594218],
                },
                viewingMode: "local",
                clippingArea: extent,
                extent: extent,
                alphaCompositingEnabled: true,
                environment: {
                    starsEnabled: false,
                    atmosphereEnabled: false,
                    background: {
                        type: "color",
                        color: [255, 54, 244, 0]
                    },
                    lighting: {
                        directShadowsEnabled: true,
                        ambientOcclusionEnabled: true,
                    }
                },
                constraints: {
                    snapToZoom: false,
                }
            });
            map.ground.opacity = 0;

            //去除logo
            view.ui.remove(["zoom", "attribution", "navigation-toggle", "compass"]);
            /**
             * 四县边界线
             */
            function text1(text) {
                var textSymbol = {
                    type: "text",
                    color: [0, 0, 255, 1],
                    haloColor: [255, 255, 255, 1],
                    haloSize: "2px",
                    text: text,
                    xoffset: 3,
                    yoffset: 3,
                    font: {
                        size: 16,
                        family: "sans-serif",
                        weight: "bold"
                    }
                };
                return textSymbol;
            };
            var JSONcOUNTRY = [{
                name: "德令哈市",
                lon: "97.291809",
                lat: '37.673523'
            }, {
                name: "祁连县",
                lon: "100.13887",
                lat: '38.019134 '
            }, {
                name: "门源县",
                lon: "101.267298",
                lat: '37.49121'
            }, {
                name: "天峻县",
                lon: "98.636347",
                lat: '37.680333'
            }]
            var graphices = [];
            for (var i = 0; i < JSONcOUNTRY.length; i++) {
                var item = JSONcOUNTRY[i];
                graphices.push({
                    geometry: {
                        type: "point",
                        latitude: item.lat,
                        longitude: item.lon,
                    },
                    symbol: text1(item.name)
                });
            }
            var countryGrap = new GraphicsLayer({
                graphics: graphices,
            });
            map.add(countryGrap);
            let COUNTY = new MapImageLayer({
                url: arcgisConfig["country"],
                id: "COUNTY",
                legendEnabled: false,
                visible: true,
                maxScale: 1000000,
                sublayers: [{
                    id: 0,
                    labelsVisible: false,
                    renderer: {
                        type: "simple", // autocasts as new SimpleRenderer()
                        symbol: {
                            type: "simple-fill",
                            color: [255, 0, 0, 0],
                            outline: {
                                width: 1.5,
                                color: [0, 0, 255, 0.6]
                            }
                        }
                    }
                }],
            });
            map.add(COUNTY);
            /**
             * 青甘省边界线
             */
            var index = obj.gshide == 0 ? obj.gshide : 1;
            var qgbjx = new MapImageLayer({
                url: arcgisConfig["qgbjx"],
                id: "qgbjx",
                legendEnabled: true,
                visible: true,
                sublayers: [{
                    id: 0,
                    visible: true,
                    labelsVisible: false,
                    renderer: {
                        type: "unique-value",
                        field: "NAME",
                        uniqueValueInfos: [{
                            value: "青海片区",
                            label: "青海片区",
                            symbol: {
                                type: "simple-fill",
                                color: [255, 255, 255, 0],
                                outline: {
                                    width: 2,
                                    color: [255, 223, 44, 1]
                                }
                            }
                        }, {
                            value: "甘肃片区",
                            label: "甘肃片区",
                            symbol: {
                                type: "simple-fill",
                                color: [255, 255, 255, 0],
                                outline: {
                                    //  style:'dash',
                                    width: 2,
                                    color: [40, 255, 90, index]
                                }
                            }
                        }]
                    }
                }]
            });
            map.add(qgbjx);
            let tempGraphicsLayer = new GraphicsLayer({
                id: 'tempGraphicsLayer'
            })
            map.add(tempGraphicsLayer);
            // var sketchVM = new SketchViewModel({
            //     layer: tempGraphicsLayer,
            //     view: view,
            //     polygonSymbol: {
            //         type: "simple-fill",
            //         color: [150, 150, 150, 0.2],
            //         outline: {
            //             color: [50, 50, 50],
            //             width: 2
            //         }
            //     }
            // });

            let samplezone = new FeatureLayer({
                url: arcgisConfig['samplezone'],
                visible: false,
                // sublayers: [{
                //   id: 0,
                renderer: {
                    type: "unique-value",
                    field: "NAME",
                    uniqueValueInfos: [{
                        value: "木里镇",
                        label: "木里镇",
                        symbol: {
                            type: "simple-fill",
                            color: [190, 232, 255, 0.3],
                            outline: {
                                width: 2,
                                color: [190, 232, 255, 1]
                            }
                        }
                    }, {
                        value: "野牛沟乡",
                        label: "野牛沟乡",
                        symbol: {
                            type: "simple-fill",
                            color: [211, 255, 190, 0.3],
                            outline: {
                                width: 2,
                                color: [211, 255, 190, 1]
                            }
                        }
                    }]
                },
                labelingInfo: [{
                        labelExpression: "[NAME]",
                        labelPlacement: "always-horizontal",
                        symbol: {
                            type: "label-3d",
                            symbolLayers: [{
                                type: "text",
                                material: { color: [211, 255, 190, 0.7] },
                                size: 12,
                                halo: {
                                    color: [0, 0, 0, 1],
                                    size: 1
                                }
                            }]
                        }
                    }]
                    // }]
            });
            map.add(samplezone);

            let sourceGraphicsLayer = new GraphicsLayer({
                id: 'sourceGraphicsLayer'
            })
            map.add(sourceGraphicsLayer);
            let sourceGraphicsLayer1 = new GraphicsLayer({
                id: 'sourceGraphicsLayer1'
            })
            map.add(sourceGraphicsLayer1);

            // view.on('click', function(res) {
            //     debugger;
            //     console.log(res);
            // })
            var mapObj = {
                view: view,
                map: map,
                FeatureLayer: FeatureLayer,
                MapImageLayer: MapImageLayer,
                watchUtils: watchUtils,
                Polyline: Polyline,
                Polygon: Polygon,
                Graphic: Graphic,
                GraphicsLayer: GraphicsLayer,
                geometryEngine: geometryEngine,
                Point: Point,
                webMercatorUtils: webMercatorUtils,
                Geoprocessor: Geoprocessor,
                FeatureSet: FeatureSet,
                LinearUnit: LinearUnit,
                PopupTemplate: PopupTemplate,
                tempGraphicsLayer: tempGraphicsLayer,
                samplezone: samplezone,
                sourceGraphicsLayer: sourceGraphicsLayer,
                sourceGraphicsLayer1: sourceGraphicsLayer1
            };
            /**
             * view 点击事件
             */
            clickView(mapObj, obj);
            resolve(mapObj);
        }).catch((err) => {
            reject(err);
        });
    });
}